package server.messageContent;

import common.Constants;
import common.DeserializationManager;
import common.data.DepositArgs;
import common.interfaces.MessageContent;

public class DepositMsgContent extends DepositArgs implements MessageContent {
	private byte[] data;
	private int length;
	
	public DepositMsgContent(byte[] data){
		this.data = data;
		this.length = data.length;
		
		int currentIndex = 0;
		byte[] holderNameLengthStream = new byte[Constants.INT_LENGTH];
		System.arraycopy(data, currentIndex, holderNameLengthStream, 0, Constants.INT_LENGTH);
		int holderNameLength = DeserializationManager.getCurrent().toInt(holderNameLengthStream);
		currentIndex += Constants.INT_LENGTH;
		
		byte[] holderNameStream = new byte[holderNameLength];
		System.arraycopy(data, currentIndex, holderNameStream, 0, holderNameLength);
		this.holderName = DeserializationManager.getCurrent().toString(holderNameStream);
		currentIndex += holderNameLength;
		
		byte[] accountNumberLengthStream = new byte[Constants.INT_LENGTH];
		System.arraycopy(data, currentIndex, accountNumberLengthStream, 0, Constants.INT_LENGTH);
		int accountNumberLength = DeserializationManager.getCurrent().toInt(accountNumberLengthStream);
		currentIndex += Constants.INT_LENGTH;
		
		byte[] accountNumberStream = new byte[accountNumberLength];
		System.arraycopy(data, currentIndex, accountNumberStream, 0, accountNumberLength);
		this.accountNumber = DeserializationManager.getCurrent().toLong(accountNumberStream);
		currentIndex += accountNumberLength;
		
		byte[] passwordLengthStream = new byte[Constants.INT_LENGTH];
		System.arraycopy(data, currentIndex, passwordLengthStream, 0, Constants.INT_LENGTH);
		int passwordLength = DeserializationManager.getCurrent().toInt(passwordLengthStream);
		currentIndex += Constants.INT_LENGTH;
		
		byte[] passwordStream = new byte[passwordLength];
		System.arraycopy(data, currentIndex, passwordStream, 0, passwordLength);
		this.password = DeserializationManager.getCurrent().toString(passwordStream);
		currentIndex += passwordLength;
		
		byte[] currencyLengthStream = new byte[Constants.INT_LENGTH];
		System.arraycopy(data, currentIndex, currencyLengthStream, 0, Constants.INT_LENGTH);
		int currencyLength = DeserializationManager.getCurrent().toInt(currencyLengthStream);
		currentIndex += Constants.INT_LENGTH;
		
		byte[] currencyStream = new byte[currencyLength];
		System.arraycopy(data, currentIndex, currencyStream, 0, currencyLength);
		this.currency = DeserializationManager.getCurrent().toCurrencyEnum(currencyStream);
		currentIndex += currencyStream.length;
		
		byte[] amountLengthStream = new byte[Constants.INT_LENGTH];
		System.arraycopy(data, currentIndex, amountLengthStream, 0, Constants.INT_LENGTH);
		int amountLength = DeserializationManager.getCurrent().toInt(amountLengthStream);
		currentIndex += Constants.INT_LENGTH;
		
		byte[] amountStream = new byte[amountLength];
		System.arraycopy(data, currentIndex, amountStream, 0, amountLength);
		this.depositAmount = DeserializationManager.getCurrent().toFloat(amountStream);
		currentIndex += amountLength;
	}
	
	@Override
	public byte[] getByte() {
		// TODO Auto-generated method stub
		return this.data;
	}

	@Override
	public int getLength() {
		// TODO Auto-generated method stub
		return this.length;
	}

}
